function getWeatherImageFromCode(code, day) {
    var day_night = "night";
    if(day) day_night = "day";
    
    var clear = [113];
    var cloudy = [119, 122];
    var fog = [248, 260];
    var partlycloudy = [116];
    var rain = [143, 176, 263, 266, 293, 296, 299, 302, 305, 308, 353, 356, 359];
    var rainsnow = [182, 185, 281, 284, 311, 314, 317, 320, 350, 362, 365, 374, 377];
    var snow = [179, 227, 230, 323, 326, 329, 332, 335, 338, 368, 371];
    var thunderstorm = [200, 286, 386, 389, 392, 395];
    
    if (clear.indexOf(code) > -1){
        return "clear_" + day_night + ".png";
    }
    if (cloudy.indexOf(code) > -1){
        return "cloudy_" + day_night + ".png";
    }
    if (fog.indexOf(code) > -1){
        return "fog_" + day_night + ".png";
    }
    if (partlycloudy.indexOf(code) > -1){
        return "partlycloudy_" + day_night + ".png";
    }
    if (rain.indexOf(code) > -1){
        return "rain_" + day_night + ".png";
    }
    if (rainsnow.indexOf(code) > -1){
        return "rainsnow_" + day_night + ".png";
    }
    if (snow.indexOf(code) > -1){
        return "snow_" + day_night + ".png";
    }
    if (thunderstorm.indexOf(code) > -1) {
        return "thunderstorm_" + day_night + ".png";
    }
}

function updateWeather(postalcode, tempUnits, distUnits, speedUnits, img, temp, weatherDesc, humidity, precipitation, visibility, cloud, pressureBar, pressure, windCompass, windSpeed) {
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
        if (doc.readyState == XMLHttpRequest.DONE) {
            var jsonObject = eval('(' + doc.responseText + ')');
            var hr = (new Date()).getHours();
            var daytime = true;
            if (hr < 6 || hr > 19) daytime = false;
            img.source = "images/weather/" + getWeatherImageFromCode(parseInt(jsonObject.data.current_condition[0].weatherCode), daytime);
            windCompass.source = "images/compass/compass_" + jsonObject.data.current_condition[0].winddir16Point + ".png";
            var press = jsonObject.data.current_condition[0].pressure;
            pressureBar.gradient.stops[2].position = (press-977) * (-1/75) + 1;
            pressureBar.gradient.stops[1].position = pressureBar.gradient.stops[2].position - 0.00001;
            
				if (tempUnits == "F") {
					 temp.text = jsonObject.data.current_condition[0].temp_F;
				}
				else {
					 temp.text = jsonObject.data.current_condition[0].temp_C;
				}
				
				if (distUnits == "mi/ft/in") {
					 pressure.text = "  " + (0.029529983*press).toFixed(1) + " inHg";
					 precipitation.text = (0.039370079*jsonObject.data.current_condition[0].precipMM).toFixed(1) + " in";
					 visibility.text = Math.round(0.62137119*jsonObject.data.current_condition[0].visibility) + " mi";
				}
				else {
					 pressure.text = "  " + press + " mb";
					 precipitation.text = Math.round(jsonObject.data.current_condition[0].precipMM) + " mm";
                visibility.text = jsonObject.data.current_condition[0].visibility + " km";
				}
				
				if (speedUnits == "mph") {
					 windSpeed.text = "  " + jsonObject.data.current_condition[0].windspeedMiles + " mph";
				}
				else {
					 windSpeed.text = "  " + jsonObject.data.current_condition[0].windspeedKmph + " kmph";
				}
                
				humidity.text = jsonObject.data.current_condition[0].humidity + "%"; 
            weatherDesc.text = jsonObject.data.current_condition[0].weatherDesc[0].value;
            cloud.text = jsonObject.data.current_condition[0].cloudcover + "%";
        }
    }
    doc.open("GET", "http://free.worldweatheronline.com/feed/weather.ashx?q="+postalcode+"&format=json&num_of_days=1&key=392cd585e9183653132801");
    doc.send();
}

function update5DayForcast(postalcode, tempUnits, dow1, date1, img1, highTemp1, lowTemp1, dow2, date2, img2, highTemp2, lowTemp2, dow3, date3, img3, highTemp3, lowTemp3, dow4, date4, img4, highTemp4, lowTemp4, dow5, date5, img5, highTemp5, lowTemp5) {
    var weekday = new Array(7);
    weekday[0] = "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
        if (doc.readyState == XMLHttpRequest.DONE) {
            var jsonObject = eval('(' + doc.responseText + ')');
            var from1 = jsonObject.data.weather[0].date.split("-");
            var d1 = new Date(from1[0], from1[1]-1, from1[2]);
            var from2 = jsonObject.data.weather[1].date.split("-");
            var d2 = new Date(from2[0], from2[1]-1, from2[2]);
            var from3 = jsonObject.data.weather[2].date.split("-");
            var d3 = new Date(from3[0], from3[1]-1, from3[2]);
            var from4 = jsonObject.data.weather[3].date.split("-");
            var d4 = new Date(from4[0], from4[1]-1, from4[2]);
            var from5 = jsonObject.data.weather[4].date.split("-");
            var d5 = new Date(from5[0], from5[1]-1, from5[2]);
            
            dow1.text = weekday[d1.getDay()].substr(0, 3);
            dow2.text = weekday[d2.getDay()].substr(0, 3);
            dow3.text = weekday[d3.getDay()].substr(0, 3);
            dow4.text = weekday[d4.getDay()].substr(0, 3);
            dow5.text = weekday[d5.getDay()].substr(0, 3);
            date1.text = (d1.getMonth()+1) + "/" + d1.getDate();
            date2.text = (d2.getMonth()+1) + "/" + d2.getDate();
            date3.text = (d3.getMonth()+1) + "/" + d3.getDate();
            date4.text = (d4.getMonth()+1) + "/" + d4.getDate();
            date5.text = (d5.getMonth()+1) + "/" + d5.getDate();
            img1.source = "images/weather/" + getWeatherImageFromCode(parseInt(jsonObject.data.weather[0].weatherCode), true);
            img2.source = "images/weather/" + getWeatherImageFromCode(parseInt(jsonObject.data.weather[1].weatherCode), true);
            img3.source = "images/weather/" + getWeatherImageFromCode(parseInt(jsonObject.data.weather[2].weatherCode), true);
            img4.source = "images/weather/" + getWeatherImageFromCode(parseInt(jsonObject.data.weather[3].weatherCode), true);
            img5.source = "images/weather/" + getWeatherImageFromCode(parseInt(jsonObject.data.weather[4].weatherCode), true);
            if (tempUnits == "F") {
                highTemp1.text = jsonObject.data.weather[0].tempMaxF + " \u00B0F";
                lowTemp1.text = jsonObject.data.weather[0].tempMinF + " \u00B0F";
                highTemp2.text = jsonObject.data.weather[1].tempMaxF + " \u00B0F";
                lowTemp2.text = jsonObject.data.weather[1].tempMinF + " \u00B0F";
                highTemp3.text = jsonObject.data.weather[2].tempMaxF + " \u00B0F";
                lowTemp3.text = jsonObject.data.weather[2].tempMinF + " \u00B0F";
                highTemp4.text = jsonObject.data.weather[3].tempMaxF + " \u00B0F";
                lowTemp4.text = jsonObject.data.weather[3].tempMinF + " \u00B0F";
                highTemp5.text = jsonObject.data.weather[4].tempMaxF + " \u00B0F";
                lowTemp5.text = jsonObject.data.weather[4].tempMinF + " \u00B0F";
            }
            else {
                highTemp1.text = jsonObject.data.weather[0].tempMaxC + " \u00B0C";
                lowTemp1.text = jsonObject.data.weather[0].tempMinC + " \u00B0C";
                highTemp2.text = jsonObject.data.weather[1].tempMaxC + " \u00B0C";
                lowTemp2.text = jsonObject.data.weather[1].tempMinC + " \u00B0C";
                highTemp3.text = jsonObject.data.weather[2].tempMaxC + " \u00B0C";
                lowTemp3.text = jsonObject.data.weather[2].tempMinC + " \u00B0C";
                highTemp4.text = jsonObject.data.weather[3].tempMaxC + " \u00B0C";
                lowTemp4.text = jsonObject.data.weather[3].tempMinC + " \u00B0C";
                highTemp5.text = jsonObject.data.weather[4].tempMaxC + " \u00B0C";
                lowTemp5.text = jsonObject.data.weather[4].tempMinC + " \u00B0C";
            }
        }
    }
    doc.open("GET", "http://free.worldweatheronline.com/feed/weather.ashx?q="+postalcode+"&format=json&num_of_days=5&key=392cd585e9183653132801");
    doc.send();
}
